package com.ant.design;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ant.utls.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private TabLayout tablayout;
    private ViewPager pager;

    private List<View> vlist = new ArrayList<>();
    private String[] titles = {"精品", "推荐", "收藏", "我的", "技术", "热门", "我的", "技术", "热门"};

    @Override
    public int getLaout() {

        return R.layout.ac_main;
    }

    @Override
    public void initView() {
        tablayout = (TabLayout) findViewById(R.id.ac_index_tab01);
        pager = (ViewPager) findViewById(R.id.ac_index_vp01);
    }

    @Override
    public void initData() {
        LayoutInflater from = LayoutInflater.from(this);
        for (int i = 0; i < titles.length; i++) {
            vlist.add(from.inflate(R.layout.fg_layout, null));
        }
        PagerAdapter adapter = new PagerAdapter() {

            @Override
            public int getCount() {
                return vlist == null ? 0 : vlist.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = vlist.get(position);
                ((TextView) (view.findViewById(R.id.fg_layout_obt01))).setText(titles[position]);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(vlist.get(position));
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        };


        pager.setAdapter(adapter);
        pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));
        tablayout.setupWithViewPager(pager);
    }

    @Override
    public void initListener() {

    }

    class PagerAdapter extends android.support.v4.view.PagerAdapter {
        private View view;

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return false;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }
    }
}
